package com.example.friendfindertwo.fragment;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.example.friendfindertwo.LoginActivity;
import com.example.friendfindertwo.R;
import com.example.friendfindertwo.adapter.FriendListAdapter;

public class FriendPageFragment extends ListFragment{
	
	public static final String ARG_PAGE = "ARG_PAGE";
	private int mPage;
	FriendListAdapter adapter;
	String[] friends = null;
	
	public static FriendPageFragment create(int page){
		Bundle args = new Bundle();
		args.putInt(ARG_PAGE, page);
		FriendPageFragment fragment = new FriendPageFragment();
		fragment.setArguments(args);
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		mPage = getArguments().getInt(ARG_PAGE);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
		menu.setHeaderTitle(friends[aInfo.position]);
		menu.add(1, 1, 1, "Delete a friend");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.friend_page, container, false);
		//TextView textView = (TextView) view.findViewById(R.id.friend_page_text_view);
		//textView.setText("Fragment #" + mPage);
		
		// TODO: fill list view: create adapter
		//String[] friends = new String[]{"Susi", "Lara", "Klaus"};

		new FriendDownloader().execute();
		
		return view;
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    int itemId = item.getItemId();
	    // Implements our logic
	    if (getUserVisibleHint()) {
	        // context menu logic
	        
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    Log.d("friends","friend to delete : "+friends[info.position]);
	    //LoginActivity.dataBaseObject.acceptRequest((String)  friends[info.position]);
	    LoginActivity.dataBaseObject.deleteFriend(friends[info.position]);
	    
	    return true;
	    }
	    return false;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		registerForContextMenu(getListView());
	}	
	
	private class FriendDownloader extends AsyncTask<Void, String[], String[]>{
		String[] friends;
		ProgressDialog myPd_ring;
		
		@Override
        protected void onPreExecute() {
			super.onPreExecute();
            myPd_ring  = new ProgressDialog (getActivity());
            myPd_ring.setMessage("loading friends");
            myPd_ring.show();
        }

		@Override
		protected String[] doInBackground(Void... params) {
			friends = new String[LoginActivity.dataBaseObject.getFriends().length];
			friends = LoginActivity.dataBaseObject.getFriends();
			FriendPageFragment.this.friends = friends;
			return friends;
		}
		
		@Override
		protected void onPostExecute(String[] result) {
			super.onPostExecute(result);
			myPd_ring.dismiss();
			if(getActivity() != null){
				//adapter = new ArrayAdapter<String>(getActivity(), R.layout.friend_list_single, result);
				adapter = new FriendListAdapter(getActivity(), result);
				setListAdapter(adapter);
			}
		}
		
	}

}
